package org.test.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  一个普通的servlet
 * @author Administrator
 * web.xml加
 * 	<!--
	 以下为servlet的内容
	-->
	<servlet>
		<servlet-name>leaveMessageServlet</servlet-name>
		<servlet-class>com.pl.HelloServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>leaveMessageServlet</servlet-name>
		<url-pattern>/leaveMessageServlet</url-pattern>
	</servlet-mapping>
 *
 */
public class LeaveMessageServlet extends HttpServlet{

	public void doPost(HttpServletRequest request,HttpServletResponse response){
		String un=request.getParameter("userName");
		String up=request.getParameter("userPassword");
		
		PrintWriter out;
		try {
			out = response.getWriter();
			System.out.println("打印出来的bytes的len为："+un.getBytes().length);
			System.out.println("jsp里的为pageEncoding=utf-8");
			System.out.println("直接打印出来的名称为："+un);
			System.out.println("经过new String(un.getBytes(),\"utf-8\")转化后名称是:"+new String(un.getBytes(),"utf-8"));
			System.out.println("经过new String(un.getBytes('utf-8'),\"utf-8\")转化后名称是:"+new String(un.getBytes("utf-8"),"utf-8"));
			System.out.println("经过new String(un.getBytes('gbk'),\"utf-8\")转化后名称是:"+new String(un.getBytes("gbk"),"utf-8"));
			System.out.println("经过new String(un.getBytes(\"ISO-8859-1\"),\"utf-8\")转化后名称是:"+new String(un.getBytes("ISO-8859-1"),"utf-8"));
			System.out.println("经过new String(un.getBytes(\"ISO-8859-1\"))转化后名称是:"+new String(un.getBytes("ISO-8859-1")));
			System.out.println("经过new String(un.getBytes(\"ISO-8859-1\"),\"gbk\")转化后名称是:"+new String(un.getBytes("ISO-8859-1"),"gbk"));
			
			response.setCharacterEncoding("utf-8");
			out.println("this is OK");
			out.print("<br/>");
			out.println("不经过任何处理，直接从前台取到out出去的名称是:"+un);
			out.print("<br/>");
			out.println("经过new String(un.getBytes(),\"utf-8\")转化后名称是:"+new String(un.getBytes(),"utf-8"));
			out.print("<br/>");
			out.println("经过new String(un.getBytes('utf-8'),\"utf-8\")转化后名称是:"+new String(un.getBytes("utf-8"),"utf-8"));
			out.print("<br/>");
			out.println("经过new String(un.getBytes('gbk'),\"utf-8\")转化后名称是:"+new String(un.getBytes("gbk"),"utf-8"));
			out.print("<br/>");
			out.println("经过new String(un.getBytes(\"ISO-8859-1\"),\"utf-8\")转化后名称是:"+new String(un.getBytes("ISO-8859-1"),"utf-8"));
			out.print("<br/>");
			out.println("经过new String(un.getBytes(\"ISO-8859-1\"))转化后名称是:"+new String(un.getBytes("ISO-8859-1")));
			out.print("<br/>");
			out.println("经过new String(un.getBytes(\"ISO-8859-1\"),\"gbk\")转化后名称是:"+new String(un.getBytes("ISO-8859-1"),"gbk"));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
//		HttpSession session = request.getSession();//得到session
//		session.setAttribute("usern", un);
//		System.out.println("session是当前会话存在的");
//		System.out.println("session的username是:"+session.getAttribute("usern"));
//		
//		
//		ServletContext application=this.getServletContext(); //得到application
//		application.setAttribute("he", "this is he");
//		System.out.println("ServletContext是 在整个web都存在的");
//		System.out.println("application的he是:"+application.getAttribute("he"));
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
}
